//
//  WBNavigationController.swift
//  itcastWeibo
//
//  Created by 谢臣 on 2019/10/15.
//  Copyright © 2019 itcast. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //隐藏默认的Navigation
        navigationBar.isHidden = true
        //setStatusBarBackgroundColor(color:.init(red: 1, green: 222/255, blue: 173/255, alpha: 0.8))
       
    }
   
    
    /*
     app启动的时候，主控制器会调用pushViewController，pushViewControWBNavigationController压栈，
     所有push方法都会调用此方法
     */
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(viewController)
        /*
         主控制器下面有导航，可以通过当前控制器在栈中的位置来判断当前控制器是否为主控制器，
         如果是主控制器则隐藏BottomBar，不是就显示
         */
        
        if children.count > 0{
            viewController.hidesBottomBarWhenPushed = true
       
        
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController{
                var title:String = "返回"
                //判断控制器的级数
                if children.count == 1{
                    //titlex显示首页的标题
                    title = children.first?.title ?? "返回"
                    
                }
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack: true)
            }
        }
       
        super.pushViewController(viewController, animated: true)
        
        
    }
    //重写返回按钮方法
    @objc private func popToParent(){
        popViewController(animated: true)
    }
    
    //设置状态栏颜色
    func setStatusBarBackgroundColor(color : UIColor){
        let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
        
        if statusBar.responds(to: #selector(setter: UIView.backgroundColor)){
            statusBar.backgroundColor = color
        }
        
    }
    
}
